home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / misc / egs.lha / EGS / EGS_Devels / Examples / Other / poly.c < prev    next >
C/C++ Source or Header  |  1993-02-17  |  6KB  |  195 lines

  1. /* star.c
  2.  *
  3.  * FM - 12.08.92
  4.  *
  5.  * Renders a moving band that changes it's colour permanently
  6.  *
  7.  *
  8.  * (c) by VIONA-Development 1992/93
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <proto/exec.h>
  13. #include <egs/egsintui.h>
  14. #include <egs/proto/egsintui.h>
  15. #include <egs/egsgfx.h>
  16. #include <egs/proto/egsgfx.h>
  17. #include <egs/egs.h>
  18. #include <egs/proto/egs.h>
  19.  
  20. long RangeRand(int i );
  21.  
  22. /* the pointers to our libraries */
  23. struct Library *EGSIntuiBase;
  24. struct Library *EGSGfxBase;
  25. struct Library *EGSBase;
  26.  
  27.  
  28. /* description of a line */
  29. struct Line
  30. {
  31.         int used;
  32.         long x1,y1;
  33.         long x2,y2;
  34. };
  35.  
  36.  
  37. /* Draw a polygon with four edges */
  38. void drawRect( EI_WindowPtr window, ULONG color,
  39.                                     struct Line *line1,
  40.                                     struct Line *line2 )
  41. {
  42.         EG_RastPortPtr rp = window->RPort;
  43.         long top  = window->BorderTop;
  44.         long left = window->BorderLeft;
  45.  
  46.         EG_SetAPen( rp, color );
  47.         EG_AreaMove( rp, left+line1->x1, top+line1->y1 );
  48.         EG_AreaDraw( rp, left+line2->x1, top+line2->y1 );
  49.         EG_AreaDraw( rp, left+line2->x2, top+line2->y2 );
  50.         EG_AreaDraw( rp, left+line1->x2, top+line1->y2 );
  51.         EG_AreaEnd( rp );
  52. }
  53.  
  54.  
  55. /* something's happening here */
  56. #define MAX_HISTORY     20
  57. #define SPEED           11
  58. #define COLORSPEED              6
  59.  
  60. void doThings( EI_WindowPtr window )
  61. {
  62.  
  63.         struct EG_AreaInfo areaInfo;
  64.         EG_Polygon areaBuffer[20];
  65.         struct Line history[MAX_HISTORY];
  66.         long x1,y1, x2,y2;
  67.         long dx1,dy1, dx2,dy2;
  68.         long dtime;
  69.         ULONG color;
  70.         ULONG dcolor;
  71.         int historyPos;
  72.         int i;
  73.  
  74.     /* allocate TmpRas */
  75.         window->RPort->AreaInfo = EG_InitArea( &areaInfo, &areaBuffer[0],20 );
  76.         window->RPort->TmpRas = E_AllocBitMap( window->Width,
  77.                                            window->Height,1,E_PIXELMAP,0,
  78.                                            window->WScreen->EScreen->Map);
  79.  
  80.     /* clear history */
  81.     for( i=0; i<MAX_HISTORY; i++ ) history[i].used = 0;
  82.  
  83.         /* Startwert bestimmen */
  84.         x1 = RangeRand( window->Width-1 );
  85.         y1 = RangeRand( window->Height-1 );
  86.         x2 = RangeRand( window->Width-1 );
  87.         y2 = RangeRand( window->Height-1 );
  88.         dtime = 1;
  89.         color = window->WinColors.Back;
  90.         if (color % 256 != 0)
  91.                 {
  92.                 color = E_GetRGB8( window->WScreen->EScreen,color);
  93.         }
  94.     /* anmimation loop */
  95.         historyPos = 1;
  96.         history[0].x1 = x1;
  97.         history[0].y1 = y1;
  98.         history[0].x2 = x2;
  99.         history[0].y2 = y2;
  100.         history[0].used = 1;
  101.         while( GetMsg( window->UserPort )==NULL )
  102.                 {
  103.         /* remove old part */
  104.                 if ( history[historyPos].used )
  105.                         {
  106.                         drawRect( window, window->WinColors.Back,
  107.                               &history[historyPos],
  108.                                           &history[(historyPos+1) % MAX_HISTORY] );
  109.                         }
  110.  
  111.         /* move everything by one step */
  112.                 dtime--;
  113.                      if ( dtime==0 )
  114.                         {
  115.                         dx1 = RangeRand( 2*SPEED ) - SPEED;
  116.                         dy1 = RangeRand( 2*SPEED ) - SPEED;
  117.                         dx2 = RangeRand( 2*SPEED ) - SPEED;
  118.                         dy2 = RangeRand( 2*SPEED ) - SPEED;
  119.                         dcolor = (((RangeRand(2*COLORSPEED) + 256 - COLORSPEED) % 256) << 24) +
  120.                      (((RangeRand(2*COLORSPEED) + 256 - COLORSPEED) % 256) << 16) +
  121.                      (((RangeRand(2*COLORSPEED) + 256 - COLORSPEED) % 256) << 8);
  122.                         dtime = RangeRand( 100 ) + 10;
  123.                         }
  124.                 x1 += dx1;
  125.                 if ( x1<0 || x1>=window->Width )  { x1 -= dx1; dx1 = -dx1; }
  126.                 y1 += dy1;
  127.                 if ( y1<0 || y1>=window->Height ) { y1 -= dy1; dy1 = -dy1; }
  128.                 x2 += dx2;
  129.                 if ( x2<0 || x2>=window->Width )  { x2 -= dx2; dx2 = -dx2; }
  130.                 y2 += dy2;
  131.                 if ( y2<0 || y2>=window->Height ) { y2 -= dy2; dy2 = -dy2; }
  132.                 color += dcolor;
  133.  
  134.         /* store the new value */
  135.                 history[historyPos].x1 = x1;
  136.                 history[historyPos].y1 = y1;
  137.                 history[historyPos].x2 = x2;
  138.                 history[historyPos].y2 = y2;
  139.                 history[historyPos].used = 1;
  140.  
  141.         /* render the new part */
  142.                 drawRect( window, color,
  143.                                   &history[(historyPos+MAX_HISTORY-1) % MAX_HISTORY],
  144.                           &history[historyPos]);
  145.  
  146.         /* move through the cyclic buffer */
  147.                 historyPos = (historyPos + 1) % MAX_HISTORY;
  148.                 }
  149. }
  150.  
  151.  
  152. /* main program */
  153. void main( int argc, char *argv[] )
  154. {
  155.         static struct EI_NewWindow newWindow =
  156.                 {
  157.                 50,30, 400,300,
  158.                 0,0, 0,0,
  159.                 NULL,
  160.                 EI_WINDOWCLOSE | EI_WINDOWBACK | EI_WINDOWDRAG,
  161.                 NULL,
  162.                 "Testfenster",
  163.                 EI_SMART_REFRESH,
  164.                 EI_iCLOSEWINDOW,
  165.                 NULL,
  166.                 {0,0,0,0,0,0,0},
  167.                 NULL,
  168.                 NULL
  169.                 };
  170.     EI_WindowPtr window;
  171.  
  172.     /* open libraries */
  173.         EGSIntuiBase = OpenLibrary( (UBYTE *)"egsintui.library", 0 );
  174.         if ( EGSIntuiBase )
  175.                 {
  176.                 EGSGfxBase = OpenLibrary( (UBYTE *)"egsgfx.library", 0 );
  177.                 if ( EGSGfxBase )
  178.                         {
  179.                         EGSBase = OpenLibrary( (UBYTE *)"egs.library", 0 );
  180.                         if ( EGSBase )
  181.                                 {
  182.                         window = EI_OpenWindow( &newWindow );
  183.                     if ( window )
  184.                                 {
  185.                                 doThings( window );
  186.                                 EI_CloseWindow( window );
  187.                                 }
  188.                                 CloseLibrary( EGSBase );
  189.                                 }
  190.                 CloseLibrary( EGSGfxBase );
  191.                 }
  192.                 CloseLibrary( EGSIntuiBase );
  193.                 }
  194. }
  195.